package com.example.lenovo.downloaddemo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.adapter.HomeAdapter;
import com.example.lenovo.downloaddemo.bean.Customer;
import com.example.lenovo.downloaddemo.bean.HomeBean;
import com.example.lenovo.downloaddemo.http.datasource.MDataSource;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.http.okhttp.utils.Collintion;
import com.example.lenovo.downloaddemo.http.okhttp.utils.CommonUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.MD5;
import com.example.lenovo.downloaddemo.http.okhttp.utils.ToastUtil;
import com.example.lenovo.downloaddemo.sqlite.LoadListCallBack;
import com.example.lenovo.downloaddemo.util.TimeUtil;
import com.example.lenovo.downloaddemo.view.TitleBarView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Request;

public class HomeFragment extends Fragment {
    @Bind(R.id.home_titleBar)
    TitleBarView titleBarView;
    @Bind(R.id.home_lv)
    ListView home_lv;
    private View view;
    private Context context;
    public static HomeFragment instance;
    private HomeAdapter adapter;
    private String allString;//拼接字符串
    public static HomeFragment getInstance() {
        if (null == instance) {
            instance = new HomeFragment();
        }
        return instance;
    }

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                String returnMessage= (String) msg.obj;
                Gson gson=new Gson();
                HomeBean bean=gson.fromJson(returnMessage,HomeBean.class);
                if(bean.getCode()==200){
                    adapter=new HomeAdapter(AppContext.getInstance(),bean.getData());
                    home_lv.setAdapter(adapter);
                }

            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (null == view) {
            view = inflater.inflate(R.layout.fragment_home, container, false);
            ButterKnife.bind(this, view);
            initView();
        } else {
            if (null != view.getParent()) {
                ((ViewGroup) view.getParent()).removeAllViews();
            }

        }
        instance = this;
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    private void initView() {
        titleBarView.setTitle(AppContext.getInstance().getResources().getString(R.string.index));
        titleBarView.showBackButton(false);
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        if(CommonUtils.isNetOk(AppContext.getInstance())){
            getIndexData();
        }else{
            ToastUtil.makeText(AppContext.getInstance(),"Fail!");
        }
    }

    private void initData() {
//        if(CommonUtils.isNetOk(AppContext.getInstance())){
//            getIndexData();
//        }

    }

    private void getIndexData(){
        /**
         * 建立请求表单，添加上传服务器的参数
         */
        HashMap<String, String> map = new HashMap<>();
        map.put("model", "index");
        map.put("method", "index");
        map.put("time",new TimeUtil().getMoment());

        Collintion collintion=new Collintion();

        allString=collintion.Sort(map)+"ad1af567dc7bfedd5e760fc1f48a59ae";
       String sign= MD5.GetMD5Code(MD5.GetMD5Code(allString));
        Log.e("拼接字符串",allString);
        Log.e("签名===",sign);
        map.put("sign",sign);
        MDataSource<Customer> dataSource = new MDataSource<>(getActivity());
        dataSource.postData(map, new LoadListCallBack() {
            @Override
            public void loadList(List list) {

            }

            @Override
            public void systemError(Request request, String errorInfo, Exception e) {
                Log.e("error", "systemError: " + errorInfo);
                e.printStackTrace();
            }

            @Override
            public void retLoad(String code) {

            }

            @Override
            public void loadString(String string) {
                Log.e("首页产品", "loadString 首页 : " + string);

                Message message=Message.obtain();
                message.what=1;
                message.obj=string;
                mHandler.sendMessage(message);





            }
        });
    }
}
